<script lang="ts" setup>
import { ref } from "vue";
const visible = ref(false);
const showModal = () => {
  visible.value = !visible.value;
};
</script>
<template>
  <Teleport to="body">
    <Transition name="fade">
      <div class="modal-wrapper" v-if="visible">
        <div class="modal-mask" @click="showModal"></div>
        <div class="modal-body">
          我是 Modal
          <button @click="showModal">关闭 Modal</button>
        </div>
      </div>
    </Transition>
  </Teleport>
  <button @click="showModal">打开 Modal</button>
</template>
<style scoped>
.modal-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-mask {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-body {
  width: 50%;
  height: 50%;
  background-color: #fff;
  padding: 20px;
  border-radius: 10px;
  z-index: 2000;
}
</style>
